package com.uxwine.alarm;


import com.uxwine.alarm.R;
import com.uxwine.alarm.main.Alarm;
import com.uxwine.alarm.main.MainFragment;
import com.uxwine.alarm.main.Timer;
import com.uxwine.alarm.more.GuideActivity;
import com.uxwine.alarm.more.MoreFragment;
import com.uxwine.view.FoldView;
import com.uxwine.view.SlideView;
import com.uxwine.view.t.DialView;
import com.uxwine.view.t.TimeView;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

public class MainActivity extends FragmentActivity implements SlideView.OnSlideListener{
	
	FrameLayout mMainView;
	FoldView mCtnMore;
	SlideView mCtnMain;
	Button mbtnShowMain;

	TimeView mtvTime;
	DialView mdvTime;
	Timer mTimer;
	int[] mnStates;
	int mnCurrent;
	MoreFragment mFrmtMore;
	MainFragment mFrmtMain;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mCtnMain = (SlideView)findViewById(R.id.main_ctn_main);
		mCtnMore = (FoldView)findViewById(R.id.main_ctn_more);
		mdvTime = (DialView)findViewById(R.id.main_dial_view);
		mtvTime = (TimeView)findViewById(R.id.main_time_view);
		
		FragmentManager fm = getSupportFragmentManager();
		mFrmtMore = (MoreFragment) fm.findFragmentById(R.id.main_frg_more);
		mFrmtMain = (MainFragment) fm.findFragmentById(R.id.main_frg_main);
		
		
		mCtnMain.setBaseView(mCtnMore);
		mCtnMain.setOnSlideListener(this);
		
		AlarmService.startService(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}	
	
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch(keyCode)
		{
		case KeyEvent.KEYCODE_VOLUME_DOWN:
			
		case KeyEvent.KEYCODE_VOLUME_UP:
			break;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onBackPressed() {
//		if(mFrmtMore.onBackPressed())
//			return;
//		if(mFrmtMain.onBackPressed())
//			return;
		if(mCtnMain.isShow())
		{
			super.onBackPressed();			
		}
		else
		{
			showMain(null);
		}
	}

	public void onClickEvent(View v)
	{
		if(v.getId() == R.id.main_btn_more)
		{
			mCtnMain.toggle();
		}
	}
	
	public void showMain(Alarm a)
	{
		mFrmtMain.editAlarm(a);
		mFrmtMain.showState(true);
		mCtnMain.toggle();
	}
	
	public void showMore()
	{
		mCtnMain.toggle();
		mFrmtMore.loadAlarms();
		Intent i = new Intent(this,GuideActivity.class);
		startActivity(i);
	}

	@Override
	public void onSliding(SlideView v, int state) {
		if(state == SlideView.VIEW_OUT)
		{
			mFrmtMain.showState(false);
			mFrmtMain.resetBuilder();
		}
	}
}
